﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.BL.Common.Query;
using BEGDC.Utility.Castle;
using BEGDC.CC.BL.Common;
using BEGDC.CC.Entity;

namespace BEGDC.CC.Web.UserControls.EV
{
	/// <summary>
	/// Summary description for SubmitEVApp.
	/// </summary>
	public class SubmitEVApp : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DropDownList ddlDirector;
		protected System.Web.UI.WebControls.Button btnSubmit;
		protected System.Web.UI.WebControls.Label lblDirector;
		protected System.Web.UI.WebControls.Label lblCATBranch;
		protected System.Web.UI.WebControls.Label lblCATBranchValue;
		protected System.Web.UI.HtmlControls.HtmlTableCell txtHint;
        protected System.Web.UI.WebControls.Button ibtnClose;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if( !IsPostBack )
			{
				InitForm();

				this.ibtnClose.Attributes["onclick"] = "javascript:OnClose();";
				this.btnSubmit.Attributes["onclick"] = "javascript:OnAssign();";
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	
		private void InitForm()
		{
			string departmentText = Request.QueryString["DeptText"];
			string departmentValue = Request.QueryString["DeptValue"];
            //departmentText = "BAH";
            //departmentValue = "BAH";

            IQuery query = AppContainer.Instance[ServiceConst.QueryService] as IQuery;

            Department dept = query.GetDepartmentByPCAT(departmentValue, true);
           
            if (dept == null)
            {
                dept = query.GetDepartmentByPCAT(departmentValue, false);
            }

			this.lblCATBranchValue.Text = departmentText;

            if (dept != null)
            {
                this.BindDirector(dept.DepartmentCode);
            }
            else
            {
                this.txtHint.InnerHtml = "There aren't any department in CAT or Province you have chosen.";
            }
		}

		private void BindDirector(string department)
		{
			BEGDC.CC.BL.UserManagement.IUser userService = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
			DataTable directorTable = userService.GetDirector( department );
			if( directorTable == null || directorTable.Rows.Count == 0 )
			{
				this.txtHint.InnerHtml = "There aren't any " + department + " approvers. Please contact role admin to add.";
			}
			else
			{
				this.ddlDirector.DataSource = directorTable;
				this.ddlDirector.DataTextField = "NameEN";
				this.ddlDirector.DataBind();

				this.txtHint.InnerHtml = "";
			}
		}

        void btnSubmit_Click(object sender, EventArgs e)
		{
			string name = this.ddlDirector.SelectedItem.Text;
			name = name.Replace(@"\", @"\\");
			Response.Write(@"<Script>returnValue='"+name+"';self.close(); </Script>");
		}
	}
}
